<template>
    <div class="add-center w-100 h-100">
        <el-card class="box-card-table h-100" shadow="never">
            <div slot="header" class="clearfix">
                <span class="fl">添加教案</span>
                <el-button class="fr" @click="goback" style="padding:6px;">返回</el-button>
            </div>
            <div class="h-100" v-loading="loading">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="主题名称：" prop="bTC_Name">
                        <el-input v-model="ruleForm.bTC_Name" placeholder="主题名称"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="学生：" prop="bSID">
                                <el-select v-model="ruleForm.bSID" placeholder="请选择学生">
                                    <el-option :label="item.SName" :value="item.SID" v-for="item in studyList" :key="item.SID"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="老师：" prop="bTID">
                                <el-select v-model="ruleForm.bTID" placeholder="请选择教师">
                                    <el-option :label="item.T_Name" :value="item.TID" v-for="item in teacherList" :key="item.TID"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="教案详情：" prop="bTC_Content">
                        <ele-editor v-model="ruleForm.bTC_Content"></ele-editor>
                    </el-form-item>
                    <el-form-item class="t-r" style="margin-top:10px;">
                        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
import elewarp from '@/components/warp-slot.vue'
import eleEditor from '@/components/editor.vue'
import {addCenterList,editCenterList,getCenterInfo,upLoadFiles,baseurl} from '@/api/index.js'
export default {
    name: 'teaching',
    components: {
        elewarp,
        eleEditor
    },
    data() {
        return {
            loading:false,
            baseurl:baseurl + '/UploadPhoto',
            ruleForm:{
                bTC_Name:'',	
                bSID:'',
                bTID:'',	
                bTC_Content:''
            },
            rules:{
                bTC_Name:[
                    { required: true, message: '主题名称不能为空', trigger: 'blur' },
                    {min:3,max:30,message:'长度再3-30个字符之间',trigger: 'blur'}
                ],
                bTC_Content:[
                    { required: true, message: '教案详情不能为空！', trigger: 'change' }
                ]
            },
        }
    },
    computed:{
        teacherList() {
            return this.$store.state.allTeacherList;
        },
        studyList() {
            return this.$store.state.allStudyList;
        }
    },
    created(){
        this.centerid = this.$route.query.id;
        if(this.centerid) {
            this._getCenterInfo();
        }
    },
    // mounted(){
    //     tinymce.init({})
    // },
    methods:{
        submitForm(){
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params ={
                        ...this.ruleForm
                    }
                    if(this.centerid) {
                        editCenterList({...params,bTCID:this.centerid}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    } else {
                        addCenterList(params).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','添加成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    }
                } else {
                    return false;
                }
            })
        },
        onReset(){
            this.$refs.searchform.resetFields()
        },
        _getCenterInfo(){
            getCenterInfo({bTCID:this.centerid}).then((res) => {
                if(res) {
                    console.log(res)
                    for (const key in this.ruleForm) {
                        if (this.ruleForm.hasOwnProperty(key)) {
                            let _key = key.substr(1);
                            if( _key in res) {
                                this.ruleForm[key] = res[_key];
                            }
                        }
                    }
                }
            })
        },

        // 全选
        goback(){
            this.$router.push({path:'/center/center-list'})
        }
    }
}
</script>
<style lang="less">
.add-center{
    .line-height{
        line-height: 20px;
    }
}
.add-center{
    .el-card__body{
        height:calc(100% - 110px);
    }
    .box-card-table{
        overflow-y: auto;
        box-sizing: border-box;
    }
    .ql-editor{
        min-height:300px;
    }
}
</style>
